package com.car.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.car.account.dto.platformResource.PlatformResourceDto;
import com.car.account.entity.Admin;
import com.car.account.entity.AdminRole;
import com.car.account.entity.PlatformPermission;
import com.car.account.entity.PlatformResource;
import com.car.account.exception.AccountException;
import com.car.account.mapper.AdminMapper;
import com.car.account.mapper.AdminRoleMapper;
import com.car.account.mapper.PlatformPermissionMapper;
import com.car.account.mapper.PlatformResourceMapper;
import com.car.account.service.PlatformResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class PlatformResourceServiceImpl extends ServiceImpl<PlatformResourceMapper, PlatformResource> implements PlatformResourceService {
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private AdminRoleMapper adminRoleMapper;
    @Resource
    private PlatformPermissionMapper platformPermissionMapper;
    @Resource
    private PlatformResourceMapper platformResourceMapper;

    @Override
    public ArrayList<PlatformResourceDto> load(Integer adminId) {
        Admin admin = adminMapper.selectById(adminId);
        if (admin == null) {
            throw new AccountException(500, "该管理员不存在");
        }
        if (!admin.getAdminState().equals(0)) {
            throw new AccountException(500, "管理员状态异常");
        }
        QueryWrapper<AdminRole> qw = new QueryWrapper<>();
        qw.eq("admin_id", adminId);
        AdminRole adminRole = adminRoleMapper.selectOne(qw);
        if (adminRole == null) {
            throw new AccountException(500, "该管理员角色异常");
        }
        QueryWrapper<PlatformPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", adminRole.getRoleId());
        List<PlatformPermission> platformPermissions = platformPermissionMapper.selectList(queryWrapper);
        List<Integer> collect = platformPermissions.stream().map(o -> o.getResourceId()).collect(Collectors.toList());
        List<PlatformResource> platformResources = platformResourceMapper.selectBatchIds(collect);
        ArrayList<PlatformResourceDto> l1 = new ArrayList<>();
        for (PlatformResource platformResource : platformResources) {
            if (platformResource.getResourceLevel().equals(1)) {
                PlatformResourceDto platformResourceDto = BeanUtil.toBean(platformResource, PlatformResourceDto.class);
                l1.add(platformResourceDto);
            }
        }
        for (PlatformResource platformResource : platformResources) {
            if (platformResource.getResourceLevel().equals(2)) {
                for (PlatformResourceDto resourceDto : l1) {
                    if (resourceDto.getResourceId().equals(platformResource.getParentId())) {
                        PlatformResourceDto platformResourceDto = BeanUtil.toBean(platformResource, PlatformResourceDto.class);
                        resourceDto.getChildren().add(platformResourceDto);
                    }
                }
            }
        }
        return l1;
    }
}
